import 'dart:io';
import 'dart:convert';
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';

// 公共配置
void _publicConfig() async {
  final dioGeneral = Dio(
    BaseOptions(
      baseUrl: "http://localhost:8012",
      connectTimeout: const Duration(seconds: 3),
      receiveTimeout: const Duration(seconds: 5),
      headers: {
        HttpHeaders.userAgentHeader: "dio",
        "api": "1.0.0"
      },
      contentType: "application/json; charset=utf-8",
      responseType: ResponseType.json
    )
  );

  final response = await dioGeneral.get("/doc/cate/all");
  print(response.data);
}

// 拦截器
void _interceptorsDemo() async {
  final dio = Dio();

  dio.interceptors.add(InterceptorsWrapper(
    onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
      handler.resolve(Response(requestOptions: options, data: "Hello 你的请求被拦截了~"));
    }
  ));

  final response = await dio.get("http://localhost:8012/doc/cate/all");
  print(response);
}

void main() async {
  _interceptorsDemo();
}